﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KZ400B
{
    public class AxleRunManager
    {
            static bool registed = false;
            public static void Regist(string aName)
            {
                if (!string.IsNullOrEmpty(aName))
                {
                    List<string> aList = new List<string>();
                    aList.Add(aName);
                    Regist(aList);
                }

            }

            public static void Regist(Dictionary<string, Axle> axleDict)
            {
                if (axleDict != null && axleDict.Count > 0)
                {
                    List<string> aList = new List<string>();
                    foreach (KeyValuePair<string, Axle> a in axleDict)
                    {
                        aList.Add(a.Key);
                    }
                    Regist(aList);
                }
            }
            

            public static void Regist(List<string> axleList)
            {
                if (axleList != null)
                {
                    lock (s_rgLock)
                    {
                        if (!registed)
                        {
                            AxleManager.OnAxleActionResponse += new AxleActionResponse(AxleManager_OnAxleActionResponse);
                        }
                        registed = true;
                    }
                    lock (s_tzLock)
                    {
                        s_tzList = new List<string>();
                    }
                    foreach (string a in axleList)
                    {
                        lock (s_tzLock)
                        {
                            s_tzList.Add(a);
                        }
                    }
                    lock (s_psLock)
                    {
                        s_psList = new List<string>(s_tzList);
                    }
                }
            }

            static void AxleManager_OnAxleActionResponse(
                string axleName
                , Axle axle
                , AxleActions action
                , bool success
                , string message)
            {
                if (action == AxleActions.ToZeroFinish)
                {
                    rmToZeroAxle(axleName);
                }
                else if (action == AxleActions.RunFinish)
                {
                    rmPsAxle(axleName);
                }
            }

            static void rmToZeroAxle(string aName)
            {
                lock (s_tzLock)
                {
                    if (s_tzList != null && s_tzList.Count > 0)
                    {
                        s_tzList.Remove(aName);
                    }
                }
            }

            static void rmPsAxle(string aName)
            {
                lock (s_psLock)
                {
                    if (s_psList != null && s_psList.Count > 0)
                    {
                        s_psList.Remove(aName);
                    }
                }
            }

            public static void UnRegist()
            {
                lock (s_rgLock)
                {
                    AxleManager.OnAxleActionResponse -= new AxleActionResponse(AxleManager_OnAxleActionResponse);
                    registed = false;
                }
            }
            static object s_rgLock = new object();
            static object s_tzLock = new object();
            static object s_psLock = new object();
            static List<string> s_tzList;
            static List<string> s_psList;

            public static bool IsToZeroReady()
            {
                lock (s_tzLock)
                {
                    if (s_tzList == null || s_tzList.Count == 0) return true;
                }

                return false;
            }

            public static bool IsPositionSettingReady()
            {
                lock (s_psLock)
                {
                    if (s_psList == null || s_psList.Count == 0) return true;
                }
                return false;
            }
        
    }
}
